package com.gzyj.jtt808.v2013.protocol.jtt1078;

import com.gzyj.jtt808.utils.HexBytesUtil;
import com.gzyj.jtt808.v2013.Package;
import com.gzyj.jtt808.v2013.enums.BusinessDataType;
import io.netty.buffer.ByteBuf;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Data
@ApiModel("文件上传指令")
public class JTT808_0x9206 extends Package implements Serializable {


    @ApiModelProperty("服务器IP地址")
    public String ServerIp;

    @ApiModelProperty("服务器端口")
    public Integer Port;

    @ApiModelProperty("用户名")
    public String UserName;

    @ApiModelProperty("密码")
    public String Password;

    @ApiModelProperty("文件上传路径")
    public String FileUploadPath;

    @ApiModelProperty("通道号")
    public Byte ChannelNo;

    @ApiModelProperty("开始时间")
    public Date BeginTime;

    @ApiModelProperty("结束时间")
    public Date EndTime;

    @ApiModelProperty("报警标志")
    public Long AlarmFlag;

    @ApiModelProperty("音视频资源类型")
    public Byte MediaType;

    @ApiModelProperty("码流类型")
    public Byte StreamType;

    @ApiModelProperty("存储位置")
    public Byte MemoryType;

    @ApiModelProperty("任务执行条件")
    public Byte TaskExcuteCondition;

    public JTT808_0x9206() {
        super(BusinessDataType.文件上传指令.getCode());
    }

    @Override
    public void encodeImpl(ByteBuf buf) {
        buf.writeByte(this.ServerIp.getBytes(Charset.forName("GBK")).length);
        buf.writeCharSequence(this.ServerIp, Charset.forName("GBK"));
        buf.writeShort(this.Port);
        buf.writeByte(this.UserName.getBytes(Charset.forName("GBK")).length);
        buf.writeCharSequence(this.UserName, Charset.forName("GBK"));
        buf.writeByte(this.Password.getBytes(Charset.forName("GBK")).length);
        buf.writeCharSequence(this.Password, Charset.forName("GBK"));
        buf.writeByte(this.FileUploadPath.getBytes(Charset.forName("GBK")).length);
        buf.writeCharSequence(this.FileUploadPath, Charset.forName("GBK"));
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
        buf.writeByte(this.ChannelNo);
        buf.writeBytes(HexBytesUtil.WriteBCD(sdf.format(this.BeginTime), 12));
        buf.writeBytes(HexBytesUtil.WriteBCD(sdf.format(this.EndTime), 12));
        buf.writeLong(this.AlarmFlag);
        buf.writeByte(this.MediaType);
        buf.writeByte(this.StreamType);
        buf.writeByte(this.MemoryType);
        buf.writeByte(this.TaskExcuteCondition);
    }

    @Override
    public JTT808_0x9206 decodeImpl(ByteBuf buf) {
        int len = Byte.toUnsignedInt(buf.readByte());
        this.ServerIp = HexBytesUtil.parseString(HexBytesUtil.readByOffset(buf, len));
        this.Port = buf.readUnsignedShort();
        len = Byte.toUnsignedInt(buf.readByte());
        this.UserName = HexBytesUtil.parseString(HexBytesUtil.readByOffset(buf, len));
        len = Byte.toUnsignedInt(buf.readByte());
        this.Password = HexBytesUtil.parseString(HexBytesUtil.readByOffset(buf, len));
        len = Byte.toUnsignedInt(buf.readByte());
        this.FileUploadPath = HexBytesUtil.parseString(HexBytesUtil.readByOffset(buf, len));
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
        this.ChannelNo = buf.readByte();
        try {
            this.BeginTime = sdf.parse(HexBytesUtil.ReadBCD(HexBytesUtil.readByOffset(buf, 6), 12));
            this.EndTime = sdf.parse(HexBytesUtil.ReadBCD(HexBytesUtil.readByOffset(buf, 6), 12));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        this.AlarmFlag = buf.readLong();
        this.MediaType = buf.readByte();
        this.StreamType = buf.readByte();
        this.MediaType = buf.readByte();
        this.TaskExcuteCondition = buf.readByte();


        return this;
    }

}
